#include "qtvrGUIApp.h"

// Check if amount of command line arguments is correct
// First parameter is the execuable file itself
// Second is the path to the image files
// Third is the column count used to place images in an 2D array
// If parameter input is not correct, the GUI is started
bool qtvrGUIApp::OnInit()
{
    wxInitAllImageHandlers();

    // no additional arguments: start GUI without exception
    if(argc==1)
    {
        wxPrintf(wxT("Starting GUI...\n\n"));
        StartGUI();
        return false;
    }

    // Invalid amount of arguments
    if(argc!=3)
    {
        wxPrintf(wxT("Invalid amount of parameters.\nStarting GUI...\n\n"));
        StartGUI();
        return false;
    }

    // Convert second parameter argv[1] from char* to wxString
    wxString imgPath(argv[1], wxConvUTF8);

    // Check if path is a valid directory
    if(!wxDirExists(imgPath))
    {
        wxPrintf(wxT("Given path is not a valid directory.\nStarting GUI...\n\n"));
        StartGUI();
        return false;
    }

    // Convert third parameter argv[2] from char* to wxString to int
    wxUint32 imgRowCount = wxAtoi(wxString(argv[2], wxConvUTF8));

    // Check if third parameter is a valid integer
    if(imgRowCount == 0)
    {
        wxPrintf(wxT("Number of rows is not a valid number.\nStarting GUI...\n\n"));
        StartGUI();
        return false;
    }

    StartConsole(imgRowCount, imgPath, imgPath+wxT("\\output.mov"));

    wxPrintf(wxT("\nOperation Completed\nPress Enter to exit"));
    wxScanf(new wxChar);

    return false;
}

void qtvrGUIApp::StartGUI()
{
    //wxMessageBox(wxT("OK"));

    //wxMainWindow* Frame = new wxMainWindow(0);
    //Frame->MakeModal(true);
    //SetTopWindow(Frame);
    //Frame->Show(1);

    qtvrGUIDialog Dialog(0);
    Dialog.ShowModal();

    //wxMessageBox(wxT("OK"));

    //wxDELETE(Frame);
}

void qtvrGUIApp::StartConsole(wxUint32 imgRowCount, wxString imgPath, wxString outputFile)
{
    wxPrintf(wxT("Scanning Directory:\n") + imgPath + wxT("\n\n"));

    wxArrayString files;
    wxDirTraverserFiles fileTraverser(files);
    wxDir(imgPath).Traverse(fileTraverser, wxT(""), wxDIR_DEFAULT);

    // support only for png, bmp, jpg, gif, tif, tga, xpm
    // remove all other files from the file list
    for(wxUint32 i=0; i<files.Count(); i++)
    {
        if(!files[i].EndsWith(wxT(".png")) && !files[i].EndsWith(wxT(".bmp")) && !files[i].EndsWith(wxT(".jpg")) && !files[i].EndsWith(wxT(".gif")) && !files[i].EndsWith(wxT(".tif")) && !files[i].EndsWith(wxT(".tga")) && !files[i].EndsWith(wxT(".xpm")))
        {
            files.RemoveAt(i);
            i--;
        }
    }

    wxUint32 imgCount = files.Count();

    if(imgCount == 0)
    {
        wxPrintf(wxT("No supported images found in the given directory (png|bmp|jpg|gif|tif|tga|xpm)\nPress Enter to exit"));
        //StartGUI();
        //return false;
        wxScanf(new wxChar);
        exit(-1);
    }

    wxUint32 imgColumnCount = imgCount/imgRowCount;

    if(imgCount<imgColumnCount || imgCount%imgColumnCount!=0)
    {
        wxPrintf(wxT("Number of rows is not a valid divisor for the number of images in the given directory\nPress Enter to exit"));
        //StartGUI();
        //return false;
        wxScanf(new wxChar);
        exit(-1);
    }

    wxImage* images = new wxImage[imgCount];

    for(wxUint32 i=0; i<imgCount; i++)
    {
        wxPrintf(wxT("Loading: ") + files[i].AfterLast('\\') + wxT("\n"));
        images[i] = wxImage(files[i]);
        // define jpg quality for this image, when exporting into the qtvr movie
        images[i].SetOption(wxT("quality"), 85);
    }

    // create an QTVR instance based on the images and save this to the output file
    QT qt(images, imgCount, imgColumnCount, imgRowCount);
    qt.save(outputFile);

    wxDELETEA(images);
}
